package arraylist;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: user
 * Date: 2024-03-26
 * Time: 18:13
 */
public class MyArrayList implements IList {
   public int[] elem ;
   public int UsedSize;
   public MyArrayList(){
       this.elem = new int[10];
   }
    @Override
    public void add(int data) {
        if(isFull()){
            expend(elem);
        }
         this.elem[UsedSize] = data;
         this.UsedSize++;
    }
  private Boolean isFull(){
       if(UsedSize == elem.length){
           return true;
       }
       return false;
  }
  private void expend(int[] elem){
      elem = Arrays.copyOf(elem,2*elem.length);

  }
    @Override
    public void add(int pos, int data) {
         try{
             checkPosOfAdd(pos);
         }
         catch(posNotLegalException e){
         e.printStackTrace();
        };
        if(isFull()){
            expend(elem);
        }
        for (int i = UsedSize; i > pos; i--) {
            elem[i] = elem[i - 1];
        }
        elem[pos] = data;
        this.UsedSize++;
    }
 private void checkPosOfAdd(int pos) throws posNotLegalException{
       if(pos < 0||pos > UsedSize){
           throw new posNotLegalException("pos位置不合法！");
       }
 }
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < UsedSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        System.out.println("顺序表不含有" + toFind);
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < UsedSize; i++) {
            if(elem[i] == toFind){
                return i;
            }
        }
        System.out.println("找不到" + toFind);
        return -1;
    }
    private void checkPosOfGetAndSet(int pos)throws posNotLegalException{
       if(pos < 0||pos >= UsedSize){
           throw new posNotLegalException("pos位置不合法！");
       }
    }
    @Override
    public int get(int pos) {
       try{
           checkPosOfGetAndSet(pos);
       }
       catch (posNotLegalException e){
           e.printStackTrace();
       }
        for (int i = 0; i < UsedSize; i++) {
            if(i == pos){
                return elem[i];
            }
        }

        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkPosOfGetAndSet(pos);
        }
        catch (posNotLegalException e){
            e.printStackTrace();
        }
        for (int i = 0; i < UsedSize; i++) {
            if(i == pos){
              elem[i] = value;
            }
        }

    }

    @Override
    public void remove(int toRemove) {
       if(elem.length == 0){
           return;
       }
       int pos = -1;
        pos = indexOf(toRemove);
        if(pos < 0){
            //System.out.println("没找到");
            return;

        }
        for (int j = pos ; j < UsedSize - 1; j++) {
            elem[j] = elem[j + 1];
        }
        this.UsedSize--;
    }

    @Override
    public void removeAll(int toRemove) {
        if(elem.length == 0){
            return;
        }
        for (int i = 0; i < UsedSize; i++) {
            int pos = -1;
            pos = indexOf(toRemove);
            if (pos < 0) {
                //System.out.println("没找到");
                return;

            }
            for (int j = pos; j < UsedSize - 1; j++) {
                elem[j] = elem[j + 1];
            }
            this.UsedSize--;
        }
    }

    @Override
    public int size() {
        return UsedSize;
    }

    @Override
    public void clear() {
   /*     for (int i = 0; i < UsedSize; i++) {
            elem[i] = null;
        }*/
    elem = null;
    UsedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < UsedSize; i++) {
            System.out.print(elem[i] + " ");

        }
    }
}
